import { Storage } from './storage.js';

export class Alibaba extends Storage {
  upload(file) {
    console.log(`Uploading ${file.name} to Aliyun OSS`);
  }
}
npm install ali-oss


<script>
  import OSS from 'ali-oss';

  const client = new OSS({
    region: 'oss-cn-hangzhou',
    accessKeyId: 'your-access-key-id',
    accessKeySecret: 'your-access-key-secret',
    bucket: '',
  });

  let file;

  async function uploadFile() {
    if (!file) {
      alert('请选择一个文件进行上传');
      return;
    }

    try {
      const uploadId = await client.initMultipartUpload(file.name);
      const partSize = 1024 * 1024; // 1MB
      const partCount = Math.ceil(file.size / partSize);

      const parts = [];
      for (let i = 0; i < partCount; i++) {
        const start = i * partSize;
        const end = Math.min(start + partSize, file.size);
        const part = file.slice(start, end);

        const result = await client.uploadPart(file.name, uploadId, i + 1, part);
        parts.push({
          number: i + 1,
          etag: result.etag,
        });
      }

      await client.completeMultipartUpload(file.name, uploadId, parts);
      alert('上传成功');
    } catch (error) {
      console.error('上传失败', error);
      alert('上传失败，请重试');
    }
  }
</script>

<input type="file" bind:this={file} />
<button on:click={uploadFile}>上传文件</button>

async function calculateFileHash(file) {
  return await computeMD5(file);
}

async function checkFileExists(file) {
  const fileHash = await calculateFileHash(file);
  // 到后端检查文件是否存在
}

const file = document.querySelector('input[type="file"]').files[0];
checkFileExists(file).then(exists => {
  if (!exists) {
  }
});
